<?php

namespace App\Admin\Controllers;

use App\Models\MonitorCharge;
use App\Models\Scene;
use Encore\Admin\Auth\Database\Role;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class MonitorChargeController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Content $content)
    {
            return $content
                ->header('景区监控负责人')
                ->body($this->grid());
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new MonitorCharge);
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->disableExport();
        $grid->disableFilter();
        $grid->id('ID')->sortable();
        $grid->name('姓名')->label();
        $grid->scene_id('景区')->using(Scene::where('is_docking',1)->pluck('title','id')->toArray());
        $grid->phone('联系电话');
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new MonitorCharge);

        $form->select('scene_id','景区')->options(Scene::where('is_docking',1)->pluck('title','id'));
        $form->text('name','姓名');
        $form->text('phone','联系电话');
        $users = Role::where('slug','monitor_admin')->first()->administrators()->pluck('name','id');
        $form->select('manager_id')->options($users);
        return $form;
    }
}
